<?php
	/**
	* Handles file upload. called from upload.php
	*/

	$result = $_FILES['file']['error'];   
  
	if ($result == 0)
	{
        $result = "File Sucessfully uploaded";
        
		$newFn = "{$_GET["to"]}/{$_FILES["file"]["name"]}";
		
		$doOvr = (isset($_POST["ovr"])) && ($_POST["ovr"] == "on");
		
		if ($doOvr)
        {
            if (move_uploaded_file($_FILES["file"]["tmp_name"], $newFn))
                echo "File sucessfully uploaded";
            else
                echo "Unable to move uploaded file"; 
        }
		else
			//
			// If the target file exists
			// return an error code of 2 which is a "File exists"
			// error code of 1 means upload went well
			// error code of 0 means there was some error during upload
			//
            if (file_exists($newFn))
                $result = "Target file already exist";
            else
            {
                if (move_uploaded_file($_FILES["file"]["tmp_name"], $newFn))
                    echo "File sucessfully uploaded";
                else
                    echo "Unable to move uploaded file";    
            }  
	}
    else
        $result = "Unknown PHP error code : " . $_FILES['file']['error'];
?>

<script type="text/javascript">
	window.top.window.finishUpload('<?php echo $result ?>');
</script>